package com.aia.ann.web.sanity;

import org.apache.log4j.Logger;

import java.io.ByteArrayOutputStream;
import java.io.File;

import net.coobird.thumbnailator.Thumbnails;


import com.aia.ilp.domain.Image;
import com.aia.ilp.domain.Thumb;
import com.aia.ilp.service.MainService;
import com.cfh.util.Tools;
import com.opensymphony.xwork2.ActionSupport;

public class TestimageAction01 extends ActionSupport {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(TestimageAction01.class);

	private static final long serialVersionUID = 1L;
	private MainService mainService;
	private File image;
	private String imageFileName;
	private String guid;

	public void validate() {
		if (imageFileName == null || imageFileName.length() == 0) {
			addFieldError("image", "图片文件过大或未选择图片");
			return;
		}
		if (imageFileName.toLowerCase().endsWith(".jpg") || imageFileName.toLowerCase().endsWith(".png")) {
		} else {
			addFieldError("image", "请选择jpg或png格式的图片");
			return;
		}
		if (image == null || image.length() == 0) {
			addFieldError("image", "选择了空文件");
			return;
		}
		if (image.length() > 1024 * 1024) {
			addFieldError("image", "您选择的文件大小超过了1024KB");
			return;
		}
	}

	/***
	 * nouse
	 */
	public String execute() {
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start"); //$NON-NLS-1$
		}
		addActionMessage("得到文件名:" + imageFileName);
		addActionMessage("原文件大小:" + image.length() + "字节");
		addActionMessage("上传至服务器的临时文件为: \r\n <br> " + image.getAbsolutePath());
		guid = Tools.GUID();
		addActionMessage("生成文件GUID:" + guid);
		{
			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
			byte[] thumbBytes = null;
			boolean sucFlg = false;
			try {
				Thumbnails.of(image).size(400, 400).outputFormat("jpg").outputQuality(0.7).toOutputStream(outputStream);

				thumbBytes = outputStream.toByteArray();
				addActionMessage("压缩成功,文件大小:" + thumbBytes.length + "字节");
				sucFlg = true;
			} catch (net.coobird.thumbnailator.tasks.UnsupportedFormatException e) {
				addActionMessage("压缩失败,该文件不是图片文件");
				logger.error("execute()", e); //$NON-NLS-1$
			} catch (Exception e) {
				addActionMessage("压缩失败" + e);
				logger.error("execute()", e); //$NON-NLS-1$
			}
			if (sucFlg) {
				Image myImage = new Image();
				myImage.setIMG_GUID(guid);
				myImage.setIMG_MAIL(guid+"@aia.com");
				myImage.setIMG_BIN(thumbBytes);
				mainService.saveImage(myImage);
				addActionMessage("保存大图成功：<image src='../custImage.jsp?guid=" + guid + "'>");
			}
		}

		{
			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
			byte[] thumbBytes = null;
			boolean sucFlg = false;
			try {
				Thumbnails.of(image).size(120, 120).outputFormat("jpg").outputQuality(0.7).toOutputStream(outputStream);
				thumbBytes = outputStream.toByteArray();
				addActionMessage("压缩成功,文件大小:" + thumbBytes.length + "字节");
				sucFlg = true;
			} catch (net.coobird.thumbnailator.tasks.UnsupportedFormatException e) {
				addActionMessage("压缩失败,该文件不是图片文件");
				logger.error("execute()", e); //$NON-NLS-1$
			} catch (Exception e) {
				addActionMessage("压缩失败" + e);
				logger.error("execute()", e); //$NON-NLS-1$
			}
			if (sucFlg) {
				Thumb myThumb = new Thumb();
				myThumb.setTHUMB_GUID(guid);
				myThumb.setTHUMB_MAIL(guid+"@aia.com");
				myThumb.setTHUMB_BIN(thumbBytes);
				mainService.saveThumb(myThumb);
				addActionMessage("保存小图成功：<image src='../custThumb.jsp?guid=" + guid + "'>");
			}
		}

		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	public void setMainService(MainService mainService) {
		this.mainService = mainService;
	}

	public MainService getMainService() {
		return mainService;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public File getImage() {
		return image;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public String getImageFileName() {
		return imageFileName;
	}

}
